#include<string>
#include<vector>
#include<unordered_map>
using namespace std;

class Solution {
    bool pyramidTransition(const string& bottom,int index, string top, const unordered_map<string, string>&map) {
        if (index>=bottom.length()-1) {
            return top == "" || pyramidTransition(top, 0, "", map);
        }
        string key = bottom.substr(index, 2);
        auto it = map.find(key);
        if (it == map.end()) return false;
        auto& val = it->second;
        for (int i = 0; i < val.length(); ++i) {
            if (pyramidTransition(bottom, index + 1, top + val[i], map))
                return true;
        }
        return false;
    }

public:
    bool pyramidTransition(string bottom, vector<string>& allowed) {
        if (bottom.length() < 2)return true;

        unordered_map<string, string>map;
        for (auto& allow : allowed) {
            map[allow.substr(0, 2)] += allow.back();
        }
        return pyramidTransition(bottom, 0, "", map);

    }
};